﻿angular.module("umbraco.resources").factory("dealerResource",
    function ($q, $http, umbRequestHelper) {
        return {
            create: function (address, name, person, contact) {
                return umbRequestHelper.resourcePromise(
                    $http.post("backoffice/UmbracoDealer/BackendDealer/Create",
                        {
                            Address: address,
                            StoreName: name,
                            ResponsiblePerson: person,
                            ContactNumber: contact
                        })
                    , "Failed to create dealer."
                );
            },
            get: function (id) {
                return umbRequestHelper.resourcePromise(
                    $http.get("backoffice/UmbracoDealer/BackendDealer/Get", { params: { id: id } })
                    , "Failed to get dealer."
                );
            },
            put: function (id, address, name, person, contact) {
                return umbRequestHelper.resourcePromise(
                    $http.put("backoffice/UmbracoDealer/BackendDealer/Put",
                        {
                            Id: id,
                            Address: address,
                            StoreName: name,
                            ResponsiblePerson: person,
                            ContactNumber: contact
                        })
                    , "Failed to edit dealer."
                );
            },
            delete: function (key) {
                return umbRequestHelper.resourcePromise(
                    $http.delete("backoffice/UmbracoDealer/BackendDealer/Delete", { params: { key: key } })
                    , "Failed to delete dealer."
                );
            },
            geocoder: function (id) {
                return umbRequestHelper.resourcePromise(
                    $http.put("backoffice/UmbracoDealer/BackendDealer/Geocoder",
                        {
                            Id: id
                        })
                    , "Failed to geocoder dealer."
                );
            },
            findAll: function (key, index, size, keyword) {
                return umbRequestHelper.resourcePromise(
                    $http.get("backoffice/UmbracoDealer/BackendDealer/FindAll",
                        {
                            params: {
                                key: key, index: index, size: size, keyword: encodeURIComponent(keyword)
                            }
                        })
                    , "Failed to get dealers."
                );
            }
        };
    });